/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

/**
 *
 * @author ari
 */
@Entity
@Table(name = "ACCION")
@XmlRootElement
@NamedQueries({
    //@NamedQuery(name = "Accion.findAll", query = "SELECT a FROM Accion a"),
    //@NamedQuery(name = "Accion.findById", query = "SELECT a FROM Accion a WHERE a.id = :id"),
    //@NamedQuery(name = "Accion.findByName", query = "SELECT a FROM Accion a WHERE a.name = :name"),
    //@NamedQuery(name = "Accion.findVentaByName", query = "SELECT a FROM Accion a WHERE a.name = :name AND a.tipo = TRUE"),
    //@NamedQuery(name = "Accion.findCompraByName", query = "SELECT a FROM Accion a WHERE a.name = :name AND a.tipo = FALSE"),
    //@NamedQuery(name = "Accion.findByInversor", query = "SELECT a FROM Accion a JOIN a.inversor i WHERE i.login = :login"),
    //@NamedQuery(name = "Accion.findVentaByInversor", query = "SELECT a.name FROM Accion a JOIN a.inversor i WHERE i.login = :login AND a.tipo = TRUE ORDER BY a.id"),
    //@NamedQuery(name = "Accion.findCompraByInversor", query = "SELECT a.name FROM Accion a JOIN a.inversor i WHERE i.login = :login AND a.tipo = FALSE ORDER BY a.id"),
    @NamedQuery(name = "Accion.findIdVentaByInversor", query = "SELECT a.id FROM Accion a JOIN a.inversor i WHERE i.login = :login AND a.tipo = TRUE ORDER BY a.id"),
    @NamedQuery(name = "Accion.findIdCompraByInversor", query = "SELECT a.id FROM Accion a JOIN a.inversor i WHERE i.login = :login AND a.tipo = FALSE ORDER BY a.id")
    //@NamedQuery(name = "Accion.findCompra", query = "SELECT a FROM Accion a JOIN a.inversor i WHERE a.name = :empresa AND i.login = :login AND a.monto = :monto AND a.tipo = FALSE"),
    //@NamedQuery(name = "Accion.findVenta", query = "SELECT a FROM Accion a JOIN a.inversor i WHERE a.name = :empresa AND i.login = :login AND a.monto = :monto AND a.tipo = TRUE"),
    //@NamedQuery(name = "Accion.findAllCompra", query = "SELECT a FROM Accion a WHERE a.tipo = FALSE"),
    //@NamedQuery(name = "Accion.findAllVenta", query = "SELECT a FROM Accion a WHERE a.tipo = TRUE"),
    //@NamedQuery(name = "Accion.findAllName", query = "SELECT DISTINCT a.name FROM Accion a")
    //@NamedQuery(name = "Accion.findVentasEmp", query = "SELECT SUM(a.num), MIN(a.monto), MAX(a.monto) FROM Accion a WHERE a.name = :empresa AND a.tipo = TRUE"),
    //@NamedQuery(name = "Accion.findComprasEmp", query = "SELECT SUM(a.num), MIN(a.monto), MAX(a.monto) FROM Accion a WHERE a.name = :empresa AND a.tipo = FALSE")
})
public class Accion implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Size(max = 255)
    @Column(name = "NAME")
    private String name;    // Nombre de la empresa
    
    @JoinColumn(name = "INVERSOR", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Inversor inversor;
    
    @Column(name = "NUM")
    private int num;    // Cantidad de acciones
    
    @Column(name = "TIPO")
    private boolean tipo;   // True si es Venta, false si es Compra

    @Column(name = "MONTO")
    private double monto;

    public double getMonto() {
        return monto;
    }

    public void setMonto(double monto) {
        this.monto = monto;
    }

    public boolean isTipo() {
        return tipo;
    }

    public void setTipo(boolean tipo) {
        this.tipo = tipo;
    }

    public Inversor getInversor() {
        return inversor;
    }

    public void setInversor(Inversor inversor) {
        this.inversor = inversor;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Accion)) {
            return false;
        }
        Accion other = (Accion) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Accion[ id=" + id + " ]";
    }
    
}
